<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>评论审核</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../source/layui/css/layui.css" media="all">
  </head>
  <body>
    <table class="layui-hide" id="test" lay-filter="test"></table>

    <script type="text/html" id="toolbarDemo">
      <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="getCheckData">获取选中行数据</button>
        <button class="layui-btn layui-btn-sm" lay-event="getCheckLength">获取选中数目</button>
        <button class="layui-btn layui-btn-sm" lay-event="isAll">验证是否全选</button>
      </div>
    </script>

    <script type="text/html" id="barDemo">
      <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="success">审核成功</button>
        <button class="layui-btn layui-btn-sm layui-btn-danger" lay-event="failed">审核失败</button>
      </div>
    </script>


    <script src="../source/layui/layui.js" type="text/javascript"></script>
    <script src="../../js/axios.min.js" type="text/javascript"></script>
    <!-- 注意：如果你直接复制所有代码到本地，上述 JS 路径需要改成你本地的 -->

    <!-- 修改日期格式 -->
    <script type="text/html" id="cTime">
      {{#
      Date.prototype.toString = function () {
      return this.getFullYear() + "-" + (this.getMonth() + 1) + "-" + this.getDate()
      + " " +this.getHours() +":" +this.getMinutes() + ":" + this.getSeconds();
      };
      }}
      {{
      new Date(d.commentDate)
      }}
    </script>

    <script>
      layui.use('table', function () {
        var table = layui.table;

        table.render({
          elem: '#test'
          , url: 'http://localhost:8081/kun/admin/index/commentPage'
          , method: "post"
          , limit: 10
          , cellMinWidth: 80 //全局定义常规单元格的最小宽度
          , toolbar: '#toolbarDemo' //开启头部工具栏，并为其绑定左侧模板
          , defaultToolbar: ['filter', 'exports', 'print', { //自定义头部工具栏右侧图标。如无需自定义，去除该参数即可
            title: '提示'
            , layEvent: 'LAYTABLE_TIPS'
            , icon: 'layui-icon-tips'
          }]
          , title: '用户数据表'
          , cols: [[
            {type: 'checkbox', fixed: 'left'}
            , {field: 'id', title: 'ID', fixed: 'left', unresize: true, sort: true}
            , {field: 'userIp', title: '用户IP地址'}
            , {field: 'title', title: '所属博客'}
            , {field: 'content', title: '评论内容'}
            , {field: 'commentDate', title: '评论时间', sort: true,toolbar: '#cTime'}
            , {fixed: 'right', title: '操作', toolbar: '#barDemo'}
          ]]
          , page: true
        });

        //监听行工具事件
        table.on('tool(test)', function (obj) {
          var comment = obj.data;//获取数据
          if (obj.event === 'success') {//成功
            comment.state = 1;//审核成功
            axios.post("http://localhost:8081/kun/admin/comment/updateComment",comment).then(res=>{
              if(res.data.code === 200){
                obj.del();
                layer.msg("审核成功");
                location.reload();//刷新页面
              }else {
                layer.alert('审核错误', {
                  icon: 5,
                  title: "提示"
                });
              }
            });
          }
          else if (obj.event === 'failed') {//失败
            comment.state = -1;//审核失败
            axios.post("http://localhost:8081/kun/admin/comment/updateComment",comment).then(res=>{
              if(res.data.code === 200){
                obj.del();
                layer.msg("该评论删除");
                location.reload();
              }else {
                layer.alert('审核错误', {
                  icon: 5,
                  title: "提示"
                });
              }
            });
          }
        });
      });
    </script>
  </body>
</html>